/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#if !defined(___GRAPHC2D_ANIMATIN_ACTION_H___)
#include "animationAction.h"
#endif

#include "object.h"
#include "texture.h"
#include "textureContainer.h"

namespace Graph2D
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	AnimationAction::AnimationAction()
		: Action(Action::TYPE_ANIMATION)
		, animation(NULL)
		, patternIndex(0)
		, patternCount(0)
		, timer(0)
		, loop(false)
	{
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	AnimationAction::~AnimationAction()
	{
		release();
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	void AnimationAction::setup(const size_t patternCount, const Animation* animation, const bool loop)
	{
		release();

		this->animation = new Animation[patternCount];
		this->patternIndex = 0;
		this->patternCount = patternCount;
		this->loop = loop;
		memcpy(this->animation, animation, sizeof(Animation) * patternCount);
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	void AnimationAction::release()
	{
		delete[] animation;
		animation = NULL;

		patternIndex = patternCount = 0;
		loop = false;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	bool AnimationAction::running() const
	{
		return loop ? true : patternIndex != (patternCount - 1);
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	bool AnimationAction::onUpdate(Object* object, const UpdateInfomation& updateInfomation)
	{
		timer += updateInfomation.deltaTime;

		while(running())
		{
			const Animation& current = animation[patternIndex];
			if(timer < current.wait)
				break;

			timer -= current.wait;
			if(patternIndex >= (patternCount - 1))
			{
				if(loop)
					patternIndex = 0;
			}
			else
			{
				++patternIndex;
			}
		}

		{
			const Animation& current = animation[patternIndex];
			object->setTexturePosition(current.uv);
			object->setTextureSize(current.wh);
		}

		return running();
	}
}
